package com.hmyd.hmyd_android.adapter;

import java.util.List;

import com.hmyd.hmyd_android.R;
import com.hmyd.hmyd_android.dto.WithdrawHistory;
import com.hmyd.hmyd_android.utils.Utils;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class WithdrawalsHistoryListAdapter extends BaseAdapter{
	
	private Context mContext;
	private List<WithdrawHistory> mPlist;
	private LayoutInflater mInflater;

	public WithdrawalsHistoryListAdapter(Context context, List<WithdrawHistory> plist){
		super();
		
		this.mContext = context;
		this.mPlist = plist;
		mInflater=LayoutInflater.from(context);
	}
	
	@Override
	public int getCount() {
		return mPlist != null ? mPlist.size() : 0;
	}

	@Override
	public WithdrawHistory getItem(int position) {
		return mPlist != null ? mPlist.get(position) : null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if(mPlist==null || mPlist.get(position)==null){
			return convertView;
		}
		
		ViewHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.view_withdrawals_history_list_item, null);
			holder = new ViewHolder();
			
			holder.account = (TextView)convertView.findViewById(R.id.account);
			holder.withdraw_status = (TextView)convertView.findViewById(R.id.withdraw_status);
			holder.withdraw_time_value = (TextView)convertView.findViewById(R.id.withdraw_time_value);
			holder.withdraw_org_value = (TextView) convertView.findViewById(R.id.withdraw_org_value);
			holder.sina_fee_value = (TextView)convertView.findViewById(R.id.sina_fee_value);
			holder.platform_fee_value = (TextView)convertView.findViewById(R.id.platform_fee_value);
			
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder) convertView.getTag();
		}
		
		WithdrawHistory withdrawHistory = getItem(position);
		if(withdrawHistory!=null){
			
			holder.account.setText(Utils.amountConversionFormat(withdrawHistory.amount));
			
			if("成功".equals(withdrawHistory.withdrawStatus)){
				holder.withdraw_status.setTextColor(mContext.getResources().getColor(R.color.home_phb_text));
				holder.withdraw_status.setText("取现成功");
			}else if("失败".equals(withdrawHistory.withdrawStatus)){
				holder.withdraw_status.setTextColor(mContext.getResources().getColor(R.color.home_rate_color));
				holder.withdraw_status.setText("取现失败");
			}else if("处理中".equals(withdrawHistory.withdrawStatus)){
				holder.withdraw_status.setTextColor(mContext.getResources().getColor(R.color.home_shf_text));
				holder.withdraw_status.setText(withdrawHistory.withdrawStatus);
			}else{
				holder.withdraw_status.setTextColor(mContext.getResources().getColor(R.color.investment_item_title_text));
				holder.withdraw_status.setText(withdrawHistory.withdrawStatus);
			}
			holder.withdraw_time_value.setText(Utils.getDateString(withdrawHistory.createdTime));
			
			if(withdrawHistory.userFee > 0) 
				holder.sina_fee_value.setText("-"+Utils.amountConversionFormat(withdrawHistory.userFee));
			 else
				holder.sina_fee_value.setText(Utils.amountConversionFormat(withdrawHistory.userFee));
			
			if(withdrawHistory.withdrawalFee > 0)
				holder.platform_fee_value.setText("-"+Utils.amountConversionFormat(withdrawHistory.withdrawalFee));
			else
				holder.platform_fee_value.setText(Utils.amountConversionFormat(withdrawHistory.withdrawalFee));
			
			double org_withdraw_value = withdrawHistory.userFee+withdrawHistory.withdrawalFee+withdrawHistory.amount;
			holder.withdraw_org_value.setText(Utils.amountConversionFormat(org_withdraw_value));
		}
		
		return convertView;
	}
	
	class ViewHolder{
		TextView account; // 实际到帐金额
		TextView withdraw_org_value; // 取现金额
		TextView withdraw_status; //取现状态
		TextView withdraw_time_value; // 取现时间
		
		TextView sina_fee_value; // 新浪手续费
		TextView platform_fee_value; // 平台手续费
	}
	
}
